{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5f74d8b2",
   "metadata": {},
   "source": [
    "# 指定变量类型\n",
    "有时您可能希望为变量指定类型。 这可以通过构造函数来完成。 Python 是一种面向对象的语言，因此它使用类来定义数据类型，包括其原始类型。\n",
    "\n",
    "因此，python 中的转换是使用构造函数完成的：\n",
    "\n",
    "int() - 从整数文字、浮点文字（通过删除所有小数）或字符串文字（提供字符串表示整数）构造整数\n",
    "\n",
    "float() - 从整数文字、浮点文字或字符串文字构造浮点数（提供字符串表示浮点数或整数）\n",
    "\n",
    "str() - 从多种数据类型构造一个字符串，包括字符串、整数文字和浮点文字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f759d7bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "x = int(1)   # x will be 1\n",
    "y = int(2.8) # y will be 2\n",
    "z = int(\"3\") # z will be 3\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "df8b5ed9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "2.8\n",
      "3.0\n",
      "4.2\n"
     ]
    }
   ],
   "source": [
    "x = float(1)     # x will be 1.0\n",
    "y = float(2.8)   # y will be 2.8\n",
    "z = float(\"3\")   # z will be 3.0\n",
    "w = float(\"4.2\") # w will be 4.2\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e45c2024",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1\n",
      "2\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "x = str(\"s1\") # x will be 's1'\n",
    "y = str(2)    # y will be '2'\n",
    "z = str(3.0)  # z will be '3.0'\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
